<?php

/**
 * Here is your custom functions.
 */

/**
 * 输出模板 function
 *
 * @param string $template 模板路径
 * @param array $vars 模板变量
 * @param string|null $app 
 * @param string|null $plugin
 * @return support\Response
 */
function fetch(
    string $template = "",
    array $vars = [],
    string $app = null,
    string $plugin = null
) {
    if ($template == "") {
        $action = request()->action;
        $controller = new \ReflectionClass(request()->controller);
        $controller = str_replace(config('app.controller_suffix', ''), "", $controller->getShortName());
        $controller = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $controller));
        $template = "$controller/$action";
    }
    return view(template: $template, vars: $vars, app: $app, plugin: $plugin);
}

/**
 * 数据模板 function
 *
 * @param array|string $name
 * @param mixed $value
 * @return void
 */
function assign(array|string $name, mixed $value = null)
{
    $request = \request();
    $plugin = $request->plugin ?? '';
    $handler = \config($plugin ? "plugin.$plugin.view.handler" : 'view.handler');
    $handler::assign($name, $value);
}

/**
 * 获取当前的response 输出类型
 *
 * @return string
 */
function getResponseType(): string
{
    return request()->acceptJson() || request()->isAjax() ? 'json' : 'html';
}

/**
 * 操作成功跳转的快捷方法
 * @access protected
 * @param  mixed $msg 提示信息
 * @param  string $url 跳转的URL地址
 * @param  mixed $data 返回的数据
 * @param  integer $wait 跳转等待时间
 * @param  array $header 发送的Header信息
 * @return void
 */
function success($msg = '', string $url = null, $data = '', int $wait = 2, array $header = [])
{
    if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
        $url = $_SERVER["HTTP_REFERER"];
    } elseif ($url) {
        $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : route($url);
    }

    $result = [
        'code' => 1,
        'msg' => $msg,
        'data' => $data,
        'url' => $url,
        'wait' => $wait,
    ];

    $type = getResponseType();
    if ('html' == strtolower($type)) {
        static $handler;
        if (null === $handler) {
            $handler = config('view.handler');
        }
        //模板路径 BASE_PATH . '/public/jump.html'
        return response($handler::render(BASE_PATH . '/public/jump.html', $result), 200, $header);
    } else {
        return json($result);
    }
}

/**
 * 操作错误跳转的快捷方法
 * @access protected
 * @param  mixed $msg 提示信息
 * @param  string $url 跳转的URL地址
 * @param  mixed $data 返回的数据
 * @param  integer $wait 跳转等待时间
 * @param  array $header 发送的Header信息
 * @return void
 */
function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
{
    if (is_null($url)) {
        $url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
    } elseif ($url) {
        $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : route($url);
    }

    $result = [
        'code' => 2,
        'msg' => $msg,
        'data' => $data,
        'url' => $url,
        'wait' => $wait,
    ];

    $type = getResponseType();

    if ('html' == strtolower($type)) {
        static $handler;
        if (null === $handler) {
            $handler = config('view.handler');
        }
        //模板路径 BASE_PATH . '/public/jump.html'
        return response($handler::render(BASE_PATH . '/public/jump.html', $result), 200, $header);
    } else {
        return json($result);
    }
}

/**
 * 验证器 function
 *
 * @param string $validate 验证类
 * @param string $scene 场景名
 * @param array $data 验证数据
 * @return boolean
 */
function validate(string $validate, string $scene, array $data): bool
{
    if (class_exists($validate)) {
        $val = new $validate;
        return $val->scene($scene)->failException(true)->check($data);
    } else {
        throw new \ErrorException("未定义验证类:$validate");
    }
}
